/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eSales.com.BL.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Home
 */
public class JaxbUtils {
    public static String userHome;
    public static String LOTS_PACKAGE = "eSales.com.BL.sales";
        public static Object unmarshal(String inputFileName, String validateSchema, Class targetClass) throws SAXException {
        try {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            JAXBContext jc = JAXBContext.newInstance(targetClass.getPackage().getName());
            Unmarshaller u = jc.createUnmarshaller();
            if(validateSchema != null) {
                File f = new File(validateSchema);
                if(f.exists()) {
                    Schema mySchema = sf.newSchema(f);
                    u.setSchema(mySchema);
                }
            }
                //Object obj = u.unmarshal(new FileInputStream(inputFileName));
                return u.unmarshal(new FileInputStream(inputFileName));
        } catch (JAXBException jbe) {
            System.out.println("*** " + jbe);
        } catch (FileNotFoundException jbe) {
            System.out.println("*** " + jbe);
        }
        return null;
    }
    
}
